package health.lm.com.mine.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.fragment.XLazyListFragment;
import health.lm.com.data.HttpCST;
import health.lm.com.mine.R;
import health.lm.com.mine.R2;
import health.lm.com.mine.adapter.MyActivitiesItemAdapter;
import health.lm.com.mine.entity.MyActivitiesEntity;
import health.lm.com.mine.present.MyActivitiesItemPresent;
import health.lm.com.router.ARouterConstant;

import static com.umeng.commonsdk.stateless.UMSLEnvelopeBuild.mContext;

/**
 * 我的活动
 */
public class MyActivitiesItemFragment extends XLazyListFragment<MyActivitiesItemPresent> {
    @BindView(R2.id.rlv_my_activities)
    RecyclerView rlvMyActivities;
    @BindView(R2.id.srl_layout)
    SmartRefreshLayout srlLayout;
    private MyActivitiesItemAdapter partAdapter;
    private List<MyActivitiesEntity.Active_list> entities;
    private String id = "";

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my_activities;
    }

    @Override
    public MyActivitiesItemPresent newP() {
        return new MyActivitiesItemPresent();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData(Bundle savedInstanceState) {

        Bundle bundle = getArguments();
        if (bundle != null) {
            id = bundle.getString("id");
        }
        initPartAdapter();
        super.rlRefreshLayout = srlLayout;
        super.recyclerView = rlvMyActivities;
        super.adapter = partAdapter;
        super.initData(savedInstanceState);
    }

    @Override
    public void loadListData(boolean isRefresh, Object layout, int page, int pageSize) {
        if(!StringUtils.isEmpty(id)){
            getP().getListMore(isRefresh,layout,id,page,pageSize);
        }
    }

    public static MyActivitiesItemFragment getInstance(String id) {
        MyActivitiesItemFragment fragment = new MyActivitiesItemFragment();
        Bundle args = new Bundle();
        args.putSerializable("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 初始化银行卡/支付宝账户
     */
    private void initPartAdapter() {
        entities = new ArrayList<>();
        partAdapter = new MyActivitiesItemAdapter(entities);
        rlvMyActivities.setLayoutManager(new LinearLayoutManager(mContext));
        rlvMyActivities.setAdapter(partAdapter);
        partAdapter.setOnItemClickListener((adapter, view, position) -> {
            MyActivitiesEntity.Active_list entity = (MyActivitiesEntity.Active_list) adapter.getData().get(position);
            withValueActivity(ARouterConstant.JSWebActivity)
                    .withString("url", entity.getJump_url())
                    .withString(HttpCST.SHARE_URL, entity.getShare_url())
                    .navigation();
        });
    }


    public void getDataSuccess(List<MyActivitiesEntity.Active_list> entities) {
        this.entities = entities;
        if (isRefresh && entities.size() >= pageSize) {
            initLoadMore();
        }
        notifyData();
    }

    private void notifyData() {
        if (entities == null) {
            return;
        }
        if (entities.size() < pageSize) {
            partAdapter.loadMoreEnd(false);
        } else {
            adapter.loadMoreComplete();
        }
        if (isRefresh) {
            partAdapter.setNewData(entities);
        } else {
            partAdapter.addData(entities);
        }
        if (entities.size()<=0){
            adapter.setEmptyView(empty());
        }
    }



}
